/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2017 SAP SE
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of SAP
 * Hybris ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the
 * terms of the license agreement you entered into with SAP Hybris.
 */
package acc.asset.sync.job;

import de.hybris.platform.cronjob.enums.CronJobResult;
import de.hybris.platform.cronjob.enums.CronJobStatus;
import de.hybris.platform.servicelayer.cronjob.AbstractJobPerformable;
import de.hybris.platform.servicelayer.cronjob.PerformResult;
import de.hybris.y2ysync.model.Y2YSyncJobModel;
import de.hybris.y2ysync.services.SyncExecutionService;
import de.hybris.y2ysync.services.SyncExecutionService.ExecutionMode;

import java.util.Optional;

import org.apache.log4j.Logger;

import acc.asset.sync.model.AccAssetExecuteSyncCronJobModel;


/**
 *
 */
public class AccAssetExecuteSyncJobPerformable extends AbstractJobPerformable<AccAssetExecuteSyncCronJobModel>
{
	private final static Logger LOG = Logger.getLogger(AccAssetExecuteSyncJobPerformable.class);
	private SyncExecutionService syncExecutionService;

	@Override
	public PerformResult perform(final AccAssetExecuteSyncCronJobModel cronJob)
	{
		final Optional<Y2YSyncJobModel> syncJobModelOp = Optional.of(cronJob.getY2YSyncJob());
		if (syncJobModelOp.isPresent())
		{
			getSyncExecutionService().startSync(syncJobModelOp.get(), ExecutionMode.SYNC);
		}
		else
		{
			LOG.warn("y2YSyncJob of this cronJob is null");
		}

		return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
	}

	/**
	 * @return the syncExecutionService
	 */
	public SyncExecutionService getSyncExecutionService()
	{
		return syncExecutionService;
	}

	/**
	 * @param syncExecutionService
	 *           the syncExecutionService to set
	 */
	public void setSyncExecutionService(final SyncExecutionService syncExecutionService)
	{
		this.syncExecutionService = syncExecutionService;
	}
}
